import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../controller/build_controller.dart';

class BuildingList extends StatefulWidget {
  const BuildingList({super.key});

  @override
  State<BuildingList> createState() => _RoomListState();
}

class _RoomListState extends State<BuildingList> {
  int size = 0;
  BuildController buildController = Get.find<BuildController>();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // 生成2-11之间的随机整数
    Random r = Random();
    size = r.nextInt(10) + 2;
    setState(() {});
  }

  // 当 数量小于5的时候 认为是单元
  // 当数量大于等于5的时候 认为是楼栋
  Widget getShowText(int index) {
    if (size < 5) {
      return Text(
          buildController.buildInfo["point"] + (index + 1).toString() + '单元');
    }
    return Text(
        buildController.buildInfo["point"] + (index + 1).toString() + '栋');
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BuildController>(builder: (controller) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('选择楼栋'),
        ),
        body: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
                onTap: () {
                  buildController.updateBuildInfo({
                    'point': '${buildController.buildInfo['point']}',
                    'building': size > 5 ? '${index + 1}单元' : '${index + 1}栋',
                  });
                  Navigator.pushNamed(context, '/room_list');
                },
                child: Container(
                  color: Colors.white,
                  padding: const EdgeInsets.all(10),
                  child: Row(
                    children: [
                      Expanded(child: getShowText(index + 1)),
                      const Row(
                        children: [
                          Icon(Icons.arrow_forward_ios,
                              size: 16, color: Colors.black),
                        ],
                      )
                    ],
                  ),
                ));
          },
          itemCount: size,
        ),
      );
    });
  }
}
